﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Drawing;
using MAVI.BillRecognition.BillDetection.source;
using System.Drawing.Imaging;
using MAVI.TagAndRecognizeObjects.StickerDetection.source;

namespace MAVI.WebService
{
    /// <summary>
    /// Summary description for MAVIWebService
    /// </summary>
    //[WebService(Namespace = "http://tempuri.org/")]
    [WebService(Namespace = "http://microsoft.com/webservices/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class MAVIWebService : System.Web.Services.WebService
    {

        //the same method for recognizing bills and recognizing stickers
        [WebMethod]
        public string RecognizeImage(Byte[] imageBytes, String s)
        {
            System.IO.MemoryStream ms = new MemoryStream(imageBytes);

            Bitmap b = (Bitmap)Image.FromStream(ms);

            RectangleDetection test = new RectangleDetection();

            String result = test.Detect(b);

            return result;

            //BillDetection test = new BillDetection();

            //test.Detect(b);

            //return "recog image";
        }

        //call the function for extracting the sticker from the image and save it on server
        [WebMethod]
        public string TagSticker(Byte[] imageBytes)
        {
            String name = "qwertyuiopasdfghjklzxcvbnmASDFGHJKLQWERTYUIOPZXCVBNM1234567890";
            String stickerName = null;
            String result;

            MemoryStream ms = new MemoryStream(imageBytes);
            
            Bitmap b = (Bitmap)Image.FromStream(ms);

            Random random = new Random();

            for (int i = 0; i < 10; i++)
            {
                stickerName += name[random.Next(0, name.Length)];
            }

            DetectSticker ds = new DetectSticker();

            ds.StickerName = stickerName;

            result = ds.Detect(b);

            return result;
        }

        [WebMethod]
        public string RecognizeSticker(Byte[] imageBytes)
        {
            MemoryStream ms = new MemoryStream(imageBytes);

            Bitmap bmp = (Bitmap)Image.FromStream(ms);

            RecognizeSticker test = new RecognizeSticker();

            String result = test.Recognize(bmp);               

            return result;
        }

        [WebMethod]
        public string HelloWorld()
        {
            return "hello Haidau!";
        }
    }
}
